/***************************************************************
*  Copyright (c) 2007 by GroupMe! Team (www.groupme.net)
*  All rights reserved.
*
*  This file is part of the GroupMe! Project. Source code of 
*  this project is closed and redistribution of this code is
*  prohibited. 
*  
*  Contact: http://www.groupme.net
*
*  This copyright notice MUST APPEAR in all copies of the file!
***************************************************************/
package net.groupme.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import net.groupme.model.Group;
import net.groupme.model.ObjectFactory;
import net.groupme.model.Resource;
import net.groupme.model.Tag;
import net.groupme.search.strategy.TagSearchStrategyAmount;
import net.groupme.search.strategy.model.WeightedIdentifiableComparatorByWeight;
import net.groupme.search.strategy.model.WeightedTag;

/**
 * created on Feb 27, 2008
 * 
 * This class provides simplified methods for computing tag clouds without explicitly 
 * querying the database, but only based on a given object, e.g. when given a group then
 * the tag cloud is computed based on <code>group.getTags()</code> and 
 * <code>resource.getTags()</code> where <code>resource</code>
 * is contained in <code>group</code>.
 * 
 * 
 * @author Fabian Abel, <a href="mailto:abel@l3s.de">abel@l3s.de</a>
 * @author last edited by: $Author: fabian $
 * 
 * @version $Revision: 1.1 $ $Date: 2008-02-27 22:37:25 $
 */
public class TagCloudComputation {

	/**
	 * Given a group this method computes the
	 * the tag cloud based on <code>group.getTags()</code> and 
	 * <code>resource.getTags()</code> where <code>resource</code>
	 * is contained in <code>group</code>.
	 * 
	 * <strong>Note:</strong> It is recommended to call this method only with
	 * In-Memory-Group-DAOs (e.g. GroupDAOMemImpl)
	 * 
	 * @param group the group for which the tag cloud should be computed.
	 */
	public static List<WeightedTag> computeTagCloud(Group group){
		List<WeightedTag> cloud = new ArrayList<WeightedTag>();
		//Tags, die der Gruppe assigned sind:
		for(Tag tag: group.getTags()){
			WeightedTag wt = new WeightedTag(tag, TagSearchStrategyAmount.WEIGHT_OF_GROUP_TAGS);
			if(cloud.contains(wt)){
				cloud.get(cloud.indexOf(wt)).weight += TagSearchStrategyAmount.WEIGHT_OF_GROUP_TAGS;
			}else{
				cloud.add(wt);
			}
		}

		//Tags, die direkt Ressourcen der Gruppe assigned sind:
		for (Resource resource : group.getResources()) {
			for(Tag tag: resource.getTags()){
				WeightedTag wt = new WeightedTag(tag, TagSearchStrategyAmount.WEIGHT_OF_RESOURCE_TAGS_WITHIN_GROUP);
				if(cloud.contains(wt)){
					cloud.get(cloud.indexOf(wt)).weight += TagSearchStrategyAmount.WEIGHT_OF_RESOURCE_TAGS_WITHIN_GROUP;
				}else{
					cloud.add(wt);
				}
			}
		}
		Collections.sort(cloud, new WeightedIdentifiableComparatorByWeight());
		return cloud;
	}
}
